﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace DL.Tools
{
    [Obsolete("")]
    public class TimerManager : Singleton<TimerManager>
    {
        [SerializeField, Header("配置计时器的数量")] private int timerCount = 10;
        [SerializeField, Header("实例化计时器的数量")] private int realTimerCount;
        private Queue<GameTimer> notWorkTimers = new Queue<GameTimer>();
        private List<GameTimer> isWorkingTimers = new List<GameTimer>();

        protected override void Construct()
        {
            for (int i = 0; i < timerCount; i++)
            {
                CreateTimer();
            }
        }

        private void Update()
        {
            UpdateTime();
        }

        private GameTimer CreateTimer()
        {
            var timer = new GameTimer();
            realTimerCount++;
            notWorkTimers.Enqueue(timer);
            return timer;
        }


        /// <summary>
        /// 计时器
        /// 获取</summary>
        /// <param name="timer">计时的时间</param>
        /// <param name="action">计时完成后调用的委托</param>
        /// <param name="scaleTimeControl">是否会受到ScaleTime影响的计时器</param>
        /// <returns>计时器</returns>
        public GameTimer GetTimer(float timer, TimerAction action, bool scaleTimeControl = false)
        {
            if (notWorkTimers.Count == 0)
            {
                CreateTimer();
            }

            GameTimer gameTimer = null;
            gameTimer = notWorkTimers.Dequeue();
            gameTimer.StartTimer(scaleTimeControl, timer, action);
            isWorkingTimers.Add(gameTimer);
            return gameTimer;
        }


        /// <summary>
        /// 停止计时器的方法
        /// </summary>
        /// <param name="gameTimer"></param>
        /// <param name="needActionDone">是否需要直接完成委托</param>
        public void CalcelTimer(GameTimer gameTimer, bool needActionDone = false)
        {
            if (gameTimer == null) return;
            //非工作计时器不能被销毁，因为可能会注册其他事件
            if (gameTimer.TimerStation != TimerStation.DoWorking) return;
            //完成委托
            gameTimer.ResetTimer(needActionDone);
            isWorkingTimers.Remove(gameTimer);
            notWorkTimers.Enqueue(gameTimer);
        }

        /// <summary>
        /// 推动WorkingTimer的计时方法，以及处理完成工作的Timer
        /// </summary>
        private void UpdateTime()
        {
            if (isWorkingTimers.Count == 0) return;

            var deltaTime = Time.deltaTime;
            var unscaledDeltaTime = Time.unscaledDeltaTime;
            for (int i = 0; i < isWorkingTimers.Count; i++)
            {
                if (isWorkingTimers[i].TimerStation == TimerStation.DoWorking)
                {
                    if (isWorkingTimers[i].IsRealTime)
                    {
                        isWorkingTimers[i].UpdateRealTimer(unscaledDeltaTime);
                    }
                    else
                    {
                        isWorkingTimers[i].UpdateTimer(deltaTime);
                    }
                }
                else if (isWorkingTimers[i].TimerStation == TimerStation.DoneWorked)
                {
                    isWorkingTimers[i].ResetTimer();
                    notWorkTimers.Enqueue(isWorkingTimers[i]);
                    isWorkingTimers.Remove(isWorkingTimers[i]);
                }
            }
        }
    }
}